<%@ page import="per.hnvcam.searchext.data.model.RuleData" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="per.hnvcam.searchext.data.model.ConditionData" %>
<%--
  Created by IntelliJ IDEA.
  User: camhoang
  Date: 3/4/12
  Time: 10:19 PM
  To change this template use File | Settings | File Templates.
--%>
<%@include file="include.jsp"%>
<%
   pageTitle = "Input Rule";
   configurationTile = "Input Rule";
%>
<%@include file="header.jsp"%>

<table border="1" cellpadding="3" cellspacing="0">
   <tr>
      <th>Human Input Rules</th>
   </tr>

   <%
      List<RuleData> baseRules = (List<RuleData>) request.getAttribute("manualRules");
      Map<String, String> classifiedClasses = (Map<String, String>) request.getAttribute("classifiedClasses");
      for (RuleData baseRule : baseRules) {%>
   <tr>
      <td>
            <span class="rule_condition_keyword">IF&nbsp;</span>
            <%
         List<ConditionData> conditionList = baseRule.getConditions();
         boolean first = true;
         for (ConditionData condition : conditionList) {
            if (!first) {%>
            <span class="rule_condition_keyword">AND&nbsp;</span>
            <%
            } else {
               first = false;
            } %>
            <span class="rule_condition_word"><%=condition.getWord()%></span> = <span class="rule_condition_value"><%=condition.getFuzzyAmount()%></span>&nbsp;
            <%
         }
         %>
            <span class="rule_condition_keyword">THEN&nbsp</span>class&nbsp;=&nbsp;
            <span class="rule_result"><%=classifiedClasses.get(baseRule.getClassifiedClass())%></span>
            <br/>

               </td>
      </tr>
            <%
      }
   %>

</table>

<br/>
<h3>New Rule:</h3>

<form id="newRule" method="post" action="<%=request.getContextPath()%>/administration/manrules.jhtml">
   <table border="0" cellpadding="3" cellspacing="0" id="humanInputRules">
      <tr>
         <td colspan="3">
            <input type="button" onclick="addConditionInput();" value="Add condition"/>
         </td>
      </tr>
      <tr>
         <td><span class="rule_condition_keyword">THEN&nbsp</span></td>
         <td colspan="2">class&nbsp;=&nbsp;<select id="ruleClass" name="ruleClass">${classChoiceList}</select></td>
      </tr>
   </table>
   <input type="hidden" id="conditionCount" name="conditionCount" />
   <input type="submit" onclick="preSubmit(event);" value="Submit Rule" />
</form>


<script type="text/javascript">
   var inputIndex = 0;

   function getConditionInputFor(id) {
      var row = document.createElement("tr");
      if (id > 0) {
         var booleanExp = document.createElement("td");
         var span = document.createElement("span");
         span.className = "rule_condition_keyword";
         span.innerHTML = "AND&nbsp";
         booleanExp.appendChild(span);
         row.appendChild(booleanExp);
      } else {
         var emptyCol = document.createElement("td");
         row.appendChild(emptyCol);
      }

      var wordCol = document.createElement("td");
      var wordInput = document.createElement("input");
      wordInput.id = "word" + id;
      wordInput.name = "word" + id;
      wordInput.nodeType = "text";
      wordCol.appendChild(wordInput);
      row.appendChild(wordCol);

      var expCol = document.createElement("td");
      expCol.innerHTML = "=";
      row.appendChild(expCol);

      var classCol = document.createElement("td");
      var classSelect = document.createElement("select");
      classSelect.id = "value" + id;
      classSelect.name = "value" + id;
      classSelect.innerHTML = "${amountChoiceList}";
      classCol.appendChild(classSelect);
      row.appendChild(classCol);

      return row;
   }

   function addConditionInput() {
      var table = document.getElementById("humanInputRules");
      var conclusion = table.lastChild;
      table.insertBefore(getConditionInputFor(inputIndex), conclusion);
      inputIndex++;
   }

   function preSubmit(event) {
      for (var i = 0; i < inputIndex; i++) {
         var wordString = trim(document.getElementById("word" + i).value);
         if (wordString.length == 0) {
            alert("Words must be not empty!");
            event.preventDefault();
            return false;
         }
      }
      document.getElementById("conditionCount").value = inputIndex;
      return true;
   }

   function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
   }


   window.onload = function() {
      addConditionInput();
   }

</script>

<%@include file="footer.jsp"%>